<?php
/**
 * Created by Boume.
 * Date: 5/2 0002
 * Time: 7:53
 */
namespace app\admin\controller;
use app\admin\model\Admin;
use app\admin\model\LoginLog;
use app\admin\model\Role;
use think\facade\Validate;
use tool\Crypt;

class User extends Base
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = input('param.limit', 20, 'intval');
            $admin = new Admin();
            $where = [];
            $param = input('param.');
            if (!empty($param['q'])) {
                switch ($param['type']) {
                    case 1:
                        $where[] = ['username','like', '%' . $param['q'] . '%'];
                        break;
                    case 2:
                        $where[] = ['id', '=', intval($param['q'])];
                        break;
                }
            }
            $role_id = input('param.role_id', 0, 'intval');
            if ($role_id > 0) {
                $where[] = ['role_id', '=', $role_id];
            }
            $lists = $admin->with('Role')->where($where)->paginate($limit, false, ['query' => input('param.')]);
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $lists->total(),
                'data' => $lists->items(),
            ]);
        } else {
            return $this->fetch();
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $admin = new Admin();
            $post = input('post.');
            $rule = [
                'username' => 'require|unique:admin,username',
                'password' => 'require|alphaDash|confirm',
                'name' => 'require',
                'mobile' => 'mobile',
                'lock' => 'number',
                'lock_msg' => 'requireIf:lock,1',
            ];
            $msg = [
                'username.require' => '用户名必填',
                'username.unique' => '用户名已存在',
                'password.require' => '密码必填',
                'password.alphaDash' => '密码只能是字母、数字、“_”、“-”',
                'password.confirm' => '两次密码不一致',
                'name.require' => '姓名必填',
                'mobile.mobile' => '手机号不正确',
                'lock.number' => '请选择正确的状态',
                'lock_msg.requireIf' => '状态锁定,备注必填',
            ];
            $validate = Validate::make($rule, $msg);
            $result = $validate->check($post);
            if (!$result) $this->error($validate->getError());
            $post['addtime'] = $post['edit_password_time'] = time();
            if($post['lock']==0) $post['lock_msg'] = '';
            $Crypt = new Crypt();
            $post['password'] = $Crypt->encrypt($post['password']);
            $admin->allowField(true)->save($post);
            return json([
                'code' => 1,
                'msg' => '添加成功',
            ]);
        } else {
            $role = Role::order('id', 'desc')->select();
            $this->assign('role', $role);
            return $this->fetch();
        }
    }

    public function edit()
    {
        $id = input('param.id', 0, 'intval');
        if ($this->request->isPost()) {
            $post = input('post.');
            $rule = [
                'password' => 'alphaDash|confirm',
                'name' => 'require',
                'mobile' => 'mobile',
                'lock' => 'number',
                'lock_msg' => 'requireIf:lock,1',
            ];
            $msg = [
                'password.alphaDash' => '密码只能是字母、数字、“_”、“-”',
                'password.confirm' => '两次密码不一致',
                'name.require' => '姓名必填',
                'mobile.mobile' => '手机号不正确',
                'lock.number' => '请选择正确的状态',
                'lock_msg.requireIf' => '状态锁定,备注必填',
            ];
            $validate = Validate::make($rule, $msg);
            $result = $validate->check($post);
            if (!$result) $this->error($validate->getError());
            if($post['lock']==0) $post['lock_msg'] = '';
            if(trim($post['password'])==""){
                unset($post['password']);
            }else{
                $Crypt = new Crypt();
                $post['password'] = $Crypt->encrypt($post['password']);
            }
            $admin = new Admin();
            $admin->allowField(true)->save($post, ['id' => $id]);
            return json([
                'code' => 1,
                'msg' => '修改成功'
            ]);
        } else {
            $role = Role::order('id', 'desc')->select();
            $this->assign('role', $role);
            $data = Admin::get($id);
            $this->assign('data', $data->getData());
            return $this->fetch();
        }
    }

    public function del()
    {
        $id = input('param.id', 0, 'intval');
        if($id==0) $this->error('非法操作');
        if($id==1) $this->error('无法删除创始人账号');
        Admin::destroy($id);
        return json([
            'code' => 1,
            'msg' => '删除成功',
            'url' => url('user/index'),
        ]);
    }

    public function login_log()
    {
        if ($this->request->isAjax()) {
            $admin_id = input('param.id', 0, 'intval');
            $limit = input('param.limit', 20, 'intval');
            $lists = LoginLog::where('admin_id', $admin_id)->order('id', 'desc')->paginate($limit, false);
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $lists->total(),
                'data' => $lists->items()
            ]);
        } else {
            return $this->fetch();
        }
    }
}